package mx.netbrain.controller;

import java.io.File;
import java.util.Date;
import java.util.List;

import org.primefaces.model.DefaultTreeNode;
import org.primefaces.model.TreeNode;

import mx.netbrain.dao.CheckinDao;
import mx.netbrain.dao.TreeDao;
import mx.netbrain.dao.impl.CheckinDaoImpl;
import mx.netbrain.dao.impl.TreeDaoImpl;
import mx.netbrain.model.Checkin;
import mx.netbrain.model.CheckinId;
import mx.netbrain.model.Repositories;
import mx.netbrain.model.Status;
import mx.netbrain.model.Tree;
import mx.netbrain.model.User;

public class TreeController {

	private static char sep = File.separatorChar;

	public static void saveTree(Repositories repository) {
		Tree tree = new Tree();
		TreeDao treeDao = new TreeDaoImpl();
		Status status = new Status();
		tree.setName(repository.getName());
		tree.setRoute(repository.getStorage().getRoute() + "3" + sep
				+ repository.getId() + sep);
		tree.setRoot(Long.parseLong("0"));
		tree.setType("Carpeta");
		tree.setRegister(new Date());
		tree.setStatus(status);
		tree.setRepositories(repository);
		treeDao.save(tree);
	}

	@SuppressWarnings("unused")
	public static TreeNode createTree(Tree node, TreeNode rootNode, User user) {
		TreeDao treeDao = new TreeDaoImpl();
		TreeNode newNode0 = new DefaultTreeNode(node, rootNode);
		List<Tree> listTree = treeDao.listNodeId(node.getId(), user);
		for (Tree treeFor : listTree) {
			TreeNode newNode1 = TreeController.createTree(treeFor, newNode0,
					user);
		}
		return newNode0;
	}

	public static void checkinFile(Tree tree, User user) {
		CheckinDao checkinDao = new CheckinDaoImpl();
		Checkin checkIn = new Checkin();
		CheckinId checkInId = new CheckinId();
		checkInId.setTree(tree.getId());
		checkInId.setUser(user.getId());
		checkIn.setId(checkInId);
		checkIn.setCheckin(true);
		checkinDao.save(checkIn);
	}

}
